package com.my.demo.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.my.annotation.Param;
import com.my.annotation.Remark;
import com.my.annotation.Return;
import com.my.demo.model.Book;
import com.my.demo.service.BookService;
import com.my.exception.Message;
import com.my.framework.dao.Pagination;
import com.my.framework.web.annotation.Page;
import com.my.framework.web.annotation.Query;
import com.my.framework.web.annotation.Update;
import com.my.framework.web.mvc.controller.BaseController;

@Controller
@RequestMapping("/demo/book")
@Remark("图书管理")
public class BookController extends BaseController{

	@Autowired
	private BookService bookService;
	
	@Page(name="返回图书首页",path="/index")
	public void index() {
		
	}
	
	@Page(name="返回新增页面",path="/prepareInsert")
	public void prepareInsert() {
		
	}
	
	@Page(name="返回修改页面",path="/prepareUpdate")
	@Param("图书Id")
	public Book prepareUpdate(Long bookId) {
		if(bookId==null) {
			throw new Message("图书Id不能为空");
		}
		Book bk= bookService.get(bookId);
		return bk;
	}
	
	
	@Update(name="新增图书",path="/insert")
	public Book insert(Book book) {
		prepareAdd(book);
		return bookService.insert(book);
	}
	
	@Update(name="通过主键删除图书",path="/deleteById")
	@Param("要删除的图书Id")
	@Return("删除记录数")
	public int deleteById(Long[] bookId) {
		return bookService.deleteById(bookId);
	}
	
	@Update(name="通过主键修改图书",path="/updateById")
	public Book updateById(Book book) {
		assertNotNull(book,"bookId");
		return bookService.updateById(book);
	}
	
	@Query(name="查询图书",path="/select")
	@Param("查询条件")
	@Param("起始条数")
	@Param("返回条数")
	public Pagination select(Book book,Integer offset,Integer limit) {
		Pagination page=bookService.selectPage(book,offset,limit);
		return page;
	}
	
	
	@Update(name="通过名称更新图书",path="/updateByBookName")
	public int updateByBookName(Book book) {
		return bookService.updateByBookName(book);
	}
}
